package com.seio.edc.controller.system;

import com.alibaba.fastjson.JSON;
import com.nova.edc.model.system.AreaPoint;
import com.nova.edc.model.system.CustomerArea;
import com.nova.edc.model.system.DeviceAir;
import com.nova.edc.model.system.PointAir;
import com.nova.edc.service.system.*;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/systemPointAir")
public class PointAirContronller {
    @Autowired
    private DeviceLightService novaDeviceLightService;

    @Autowired
    private PointAirService pointAirService;

    @Autowired
    private AreaPointService novaAreaPointService;

    @Autowired
    private com.nova.edc.service.system.CustomerInfoService novaCustomerInfoService;

    @Autowired
    private CustomerAreaService novaCustomerAreaService;

    @Autowired
    private DeviceAirService deviceAirService;

    private static final int PAGE_SIZE = 10;

    // 初始化灯具管理页面
    @RequestMapping(value = "pointAirList.shtml")
    public ModelAndView pointAirList(HttpServletRequest request,
                                     HttpSession session, Integer pageIndex, PointAir pointAir,
                                     ModelMap modelMap) {
        // 当前页数
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        PageList<PointAir> pageList = pointAirService.findPointAirPageList(pointAir, page, "id", "asc");
        AreaPoint areaPoint = novaAreaPointService.findById(pointAir.getPointid());
        CustomerArea customerArea = novaCustomerAreaService.findById(areaPoint.getAreaid());
        modelMap.put("pointAir", pointAir);
        modelMap.put("customerArea", customerArea);
        modelMap.put("areaPoint", areaPoint);
        modelMap.put("pointAirs", pageList.getDatas());
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        return new ModelAndView("system/pointAirList", modelMap);
    }

    // 编辑空调页面
    @RequestMapping(value = "pointAirEdit.shtml")
    public ModelAndView pointAirEdit(HttpServletRequest request,
                                     HttpSession session, Integer pointid, Integer id, ModelMap modelMap) {
        if (id != null && id != 0) {
            PointAir pointAir = pointAirService.findById(id);
            modelMap.put("pointAir", pointAir);
        }
        AreaPoint areaPoint = novaAreaPointService.findById(pointid);
        CustomerArea customerArea = novaCustomerAreaService.findById(areaPoint.getAreaid());
        List<DeviceAir> deviceAirList = deviceAirService.findAll();
        if (deviceAirList != null && deviceAirList.size() > 0) {
            modelMap.put("deviceAirInfo", JSON
                    .toJSONString(deviceAirService.findCodeByList(deviceAirList)));
        }
        modelMap.put("customerArea", customerArea);
        modelMap.put("areaPoint", areaPoint);
        return new ModelAndView("system/pointAirEdit", modelMap);
    }

    //		 确认更改
    @RequestMapping(value = "savePointAir.shtml")
    public ModelAndView savePointAir(HttpServletRequest request,
                                     HttpSession session, PointAir pointAir, ModelMap modelMap) {
        if (pointAir.getId() != null && pointAir.getId() != 0) {
            pointAirService.update(pointAir);
        } else {
            pointAirService.insert(pointAir);
        }
        PointAir pointAir1 = new PointAir();
        pointAir1.setPointid(pointAir.getPointid());
        return pointAirList(request, session, 1, pointAir1, modelMap);
    }

    //删除
    @RequestMapping(value = "delPointAir.shtml")
    public ModelAndView delPointAir(HttpServletRequest request,
                                    HttpSession session, Integer pointid, Integer id,
                                    ModelMap modelMap) {
        pointAirService.deleteById(id);
        PointAir pointAir1 = new PointAir();
        pointAir1.setPointid(pointid);
        return pointAirList(request, session, 1, pointAir1, modelMap);

    }
}
